#!/usr/bin/python
import sys
import re
import urllib
import urllib2
def main(host,port,type,args):
	# the next line is probably the most skewed way of assinging a default value of 80 to port
	print args
	resplist=[]
	for arg in args:
		print arg
		ops = re.split(",",arg)
		for op in ops:
			optype = None
			optid=None
			opdi = None
			val = None
			optype,optid,opdi,val = ((re.split("-",op))+[None,None])[:4]
			#print ("Optype: %s for transaction %s with val: %s" % (optype,optid,val))
			reqparams = {}
			reqparams["txn"] = optid
			reqparams["opn"] = optype
			reqparams["key"] = opdi
			reqparams["value"] = val
			reqparams["ct"] = "text"
			params=urllib.urlencode(reqparams)
			if type == "occ":
				request = urllib2.Request("http://"+host+":"+port+"/addOCCTrans",data=params)
			else:
				request = urllib2.Request("http://"+host+":"+port+"/addTransaction",data=params)

			res = urllib2.urlopen(request)
			resp = res.read()
			print resp
			resplist.append(resp)
			template="http://my_app.appspot.com?txn=txn_id&opn=op_type&key=data_item_key&value=data_item_value";
			#return resp

	if len(resplist) == 1:
		return resplist[0]
	else:
		return resplist
			


if __name__ == "__main__":
	try:
		host,port = (re.split(":",sys.argv[1]) + ["80"])[:2]
		type=sys.argv[2]
		main(host,port,type,sys.argv[3:])
	except IndexError:
		print "Usage $./gnreq.py host:[port=80] occ/ss2pl ops"
